package Java.Array;
//稀疏数组
public class Demo04 {
    public static void main(String[] args) {
        int[][] arrays = new int[11][11];
        arrays[1][2] = 2;
        arrays[2][3] = 3;
        arrays[3][4] = 4;
        arrays[4][5] = 5;
        arrays[5][6] = 6;
        arrays[6][7] = 7;
        arrays[7][8] = 8;
        arrays[7][9] = 9;
        arrays[8][9] = 10;
//        System.out.println();
//        1.打印稀疏数组
//        printArray(arrays);
//          2.转换为稀疏数组

//        计算有效值个数
        int counts= 0;
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays[0].length; j++) {
                if (arrays[i][j] != 0) {
                    counts++;
                }
            }
        }
        System.out.println(counts);
        int[][] arrays2 = new int[counts+1][3];
        arrays2[0][0] = arrays.length;
        arrays2[0][1] = arrays[0].length;
        arrays2[0][2] = counts;
        int sums=1;
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays[0].length; j++) {
                if (arrays[i][j] != 0) {
                   arrays2[sums][0] = i;
                   arrays2[sums][1] = j;
                   arrays2[sums][2] = arrays[i][j];
                   sums++;
                }
            }
        }
//        输出稀疏数组
//        for (int i = 0; i < arrays2.length; i++) {
//            System.out.print(arrays2[i][0]+"\t");
//            System.out.print(arrays2[i][1]+"\t");
//            System.out.print(arrays2[i][2]+"\t");
//            System.out.println();
//        }

//            3.还原稀疏数组
        int[][] arrays3 = new int[arrays2[0][0]][arrays2[0][1]];
        for (int i = 1; i < arrays2.length; i++) {
            arrays3[arrays2[i][0]][arrays2[i][1]] = arrays2[i][2];

        }
        printArray(arrays3);
    }






    public static void printArray(int[][] array) {
        for (int[] array1 : array) {
            for (int array2 : array1) {
                System.out.print(array2 + " ");
            }
            System.out.println();
        }
    }
}


